package com.tudou.searchcontrol.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReadPage {
	
	private HttpServletRequest request;
    private HttpServletResponse response;
    
    private ReadPage(){};
    
	public ReadPage(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }
	
	public String include(String includePath,String encoding) {
		StringBuffer str = new StringBuffer(); 

		response.setContentType("text/html");
		if(encoding == null){
			encoding = "gbk";
		}
		response.setCharacterEncoding(encoding);
		
		try {
			URL url = new URL(includePath);
			HttpURLConnection connection= (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(2000);  
			//Get Response	
		      InputStream is = connection.getInputStream();
		      BufferedReader rd = new BufferedReader(new InputStreamReader(is,encoding));
		      String line;
		     
		      while((line = rd.readLine()) != null) {
		    	  
		    	  str.append(line);
		    	  str.append('\r');
		      }
		      rd.close();

		      
		} catch (MalformedURLException e) {
			System.out.println("url:"+includePath);
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("url:"+includePath);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return str.toString();
		
	}
}
